FROM php:8.2.25-fpm-alpine3.20
#
LABEL org.opencontainers.image.authors="Wang Junhua(tangramor@gmail.com)"
LABEL org.opencontainers.image.url="https://www.github.com/tangramor/nginx-php8-fpm"

# China alpine mirror: mirrors.ustc.edu.cn
ARG APKMIRROR=dl-cdn.alpinelinux.org

USER root

WORKDIR /app

ENV TZ Asia/Shanghai

# China php composer mirror: https://mirrors.cloud.tencent.com/composer/
ENV COMPOSERMIRROR "https://mirrors.cloud.tencent.com/composer/"

COPY conf/supervisord.conf /etc/supervisord.conf
COPY conf/php-fpm.conf /etc/supervisor/conf.d/php-fpm.conf
COPY conf/nginx.conf /etc/nginx/nginx.conf
COPY conf/default.conf /etc/nginx/conf.d/default.conf
# COPY conf/resolv.conf /etc/resolv.conf

COPY start.sh /app/start.sh

ENV PHP_MODULE_DEPS libc-dev openssl-dev pcre-dev zlib-dev libpng-dev libjpeg-turbo-dev freetype-dev icu-dev libzip-dev libxml2-dev
#gcc make libc-dev rabbitmq-c-dev zlib-dev libmemcached-dev cyrus-sasl-dev libpng-dev libxml2-dev krb5-dev curl-dev icu-dev libzip-dev openldap-dev imap-dev postgresql-dev libjpeg-turbo-dev freetype-dev libwebp-dev gettext-dev imagemagick-dev
# ENV MUSL_LOCALE_DEPS cmake make musl-dev gcc gettext-dev libintl 
# ENV MUSL_LOCPATH /usr/share/i18n/locales/musl

ENV NGINX_VERSION 1.27.1
ENV NGINX_PKG_RELEASE 1
ENV NJS_VERSION   0.8.5
ENV PKG_RELEASE   2
ENV COMPOSER_ALLOW_SUPERUSER 1

RUN apk add nginx \
    # Bring in tzdata so users could set the timezones through the environment
    # variables
    && apk add --no-cache tzdata \
    # Bring in curl and ca-certificates to make registering on DNS SD easier
    && apk add --no-cache curl ca-certificates \
    # forward request and error logs to docker log collector
    && ln -sf /dev/stdout /var/log/nginx/access.log \
    && ln -sf /dev/stderr /var/log/nginx/error.log

ENV fpm_conf /usr/local/etc/php-fpm.d/www.conf
ENV php_vars /usr/local/etc/php/conf.d/docker-vars.ini

RUN echo "cgi.fix_pathinfo=0" > ${php_vars} &&\
    echo "upload_max_filesize = 100M"  >> ${php_vars} &&\
    echo "post_max_size = 100M"  >> ${php_vars} &&\
    echo "variables_order = \"EGPCS\""  >> ${php_vars} && \
    echo "memory_limit = 128M"  >> ${php_vars} && \
    sed -i \
    -e "s/;catch_workers_output\s*=\s*yes/catch_workers_output = yes/g" \
    -e "s/pm.max_children = 5/pm.max_children = 64/g" \
    -e "s/pm.start_servers = 2/pm.start_servers = 8/g" \
    -e "s/pm.min_spare_servers = 1/pm.min_spare_servers = 8/g" \
    -e "s/pm.max_spare_servers = 3/pm.max_spare_servers = 32/g" \
    -e "s/;pm.max_requests = 500/pm.max_requests = 800/g" \
    -e "s/user = www-data/user = nginx/g" \
    -e "s/group = www-data/group = nginx/g" \
    -e "s/;listen.mode = 0660/listen.mode = 0666/g" \
    -e "s/;listen.owner = www-data/listen.owner = nginx/g" \
    -e "s/;listen.group = www-data/listen.group = nginx/g" \
    -e "s/listen = 127.0.0.1:9000/listen = \/var\/run\/php-fpm.sock/g" \
    -e "s/^;clear_env = no$/clear_env = no/" \
    ${fpm_conf} \
    && cp /usr/local/etc/php/php.ini-production /usr/local/etc/php/php.ini
# && sed -i 's/session.save_handler = files/session.save_handler = redis\nsession.save_path = "tcp:\/\/redis:6379"/g' /usr/local/etc/php/php.ini

RUN curl http://getcomposer.org/installer | php -- --install-dir=/usr/bin/ --filename=composer
    # && apk add --no-cache libstdc++ mysql-client bash bash-completion shadow linux-headers \
    # supervisor git zip unzip coreutils libpng libmemcached-libs krb5-libs icu-libs \
    # icu c-client libzip openldap-clients imap postgresql-client postgresql-libs libcap tzdata sqlite \
    # lua-resty-core nginx-mod-http-lua rabbitmq-c gettext imagemagick \
    
    # runtime require
RUN apk add libpng libjpeg libzip freetype icu-libs 
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone \
    && set -xe \
    # && apk add --no-cache --update --virtual .phpize-deps $PHPIZE_DEPS \
    && apk add --virtual .all-deps $PHP_MODULE_DEPS \
    && docker-php-ext-configure gd --with-jpeg --with-freetype \
    && docker-php-ext-install exif gd intl pdo pdo_mysql zip fileinfo dom  \
    # && printf "\n\n" | pecl install amqp \
    # && docker-php-ext-enable amqp \
    # && printf "\n\n\n\n" | pecl install -o -f redis \
    && docker-php-ext-enable gd \
    # && docker-php-ext-enable sockets \
    # && pecl install msgpack && docker-php-ext-enable msgpack \
    # && pecl install igbinary && docker-php-ext-enable igbinary \
    # && pecl install swoole && docker-php-ext-enable swoole \
    # && printf "\n\n\n\n\n\n\n\n\n\n" | pecl install memcached \
    # && docker-php-ext-enable memcached \
    # && pecl install mongodb \
    # && docker-php-ext-enable mongodb \
    # && printf "\n" | pecl install imagick \
    # && docker-php-ext-enable imagick \
    && rm -rf /tmp/pear \
    && apk del .all-deps \
    && composer config -g repos.packagist composer https://mirrors.cloud.tencent.com/composer/ \
    # && if [ "$COMPOSERMIRROR" != "" ]; then composer config -g repos.packagist composer ${COMPOSERMIRROR}; fi \
    && rm -rf /var/cache/apk/* /tmp/* /var/tmp/* \
    && rm -f /etc/nginx/conf.d/default.conf.apk-new && rm -f /etc/nginx/nginx.conf.apk-new \
    && if [ "$APKMIRROR" != "dl-cdn.alpinelinux.org" ]; then sed -i 's/'$APKMIRROR'/dl-cdn.alpinelinux.org/g' /etc/apk/repositories; fi \
    && set -ex \
    # && setcap 'cap_net_bind_service=+ep' /usr/local/bin/php \
    && mkdir -p /var/log/supervisor

EXPOSE 443 80

CMD ["sh","start.sh"]
